package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Proxy_Request类
 * 实现代理请求的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Proxy_Request implements AxdrType {

    private byte[] dataCode = null;
    private ProxyChoice choice = ProxyChoice._ERR_NONE_SELECTED;

    private ProxyGetRequestList proxyGetRequestList = null;
    private ProxyGetRequestRecord proxyGetRequestRecord = null;
    private ProxySetRequestList proxySetRequestList = null;
    private ProxySetThenGetRequestList proxySetThenGetRequestList = null;
    private ProxyActionRequestList proxyActionRequestList = null;
    private ProxyActionThenGetRequestList proxyActionThenGetRequestList = null;
    private ProxyTransCommandRequest proxyTransCommandRequest = null;

    /**
     * ProxyChoice枚举
     * 定义代理请求的选择枚举值
     */
    public enum ProxyChoice {
        _ERR_NONE_SELECTED(-1),
        PROXY_GET_REQUEST_LIST(1),
        PROXY_GET_REQUEST_RECORD(2),
        PROXY_SET_REQUEST_LIST(3),
        PROXY_SET_THEN_GET_REQUEST_LIST(4),
        PROXY_ACTION_REQUEST_LIST(5),
        PROXY_ACTION_THEN_GET_REQUEST_LIST(6),
        PROXY_TRANS_COMMAND_REQUEST(7);

        private final int value;

        ProxyChoice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static ProxyChoice fromValue(int value) {
            for (ProxyChoice choice : ProxyChoice.values()) {
                if (choice.value == value) {
                    return choice;
                }
            }
            return _ERR_NONE_SELECTED;
        }
    }

    public Proxy_Request() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setProxyGetRequestList(ProxyGetRequestList proxyGetRequestList) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_GET_REQUEST_LIST;
        this.proxyGetRequestList = proxyGetRequestList;
    }

    public void setProxyGetRequestRecord(ProxyGetRequestRecord proxyGetRequestRecord) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_GET_REQUEST_RECORD;
        this.proxyGetRequestRecord = proxyGetRequestRecord;
    }

    public void setProxySetRequestList(ProxySetRequestList proxySetRequestList) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_SET_REQUEST_LIST;
        this.proxySetRequestList = proxySetRequestList;
    }

    public void setProxySetThenGetRequestList(ProxySetThenGetRequestList proxySetThenGetRequestList) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_SET_THEN_GET_REQUEST_LIST;
        this.proxySetThenGetRequestList = proxySetThenGetRequestList;
    }

    public void setProxyActionRequestList(ProxyActionRequestList proxyActionRequestList) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_ACTION_REQUEST_LIST;
        this.proxyActionRequestList = proxyActionRequestList;
    }

    public void setProxyActionThenGetRequestList(ProxyActionThenGetRequestList proxyActionThenGetRequestList) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_ACTION_THEN_GET_REQUEST_LIST;
        this.proxyActionThenGetRequestList = proxyActionThenGetRequestList;
    }

    public void setProxyTransCommandRequest(ProxyTransCommandRequest proxyTransCommandRequest) {
        this.resetChoices();
        this.choice = ProxyChoice.PROXY_TRANS_COMMAND_REQUEST;
        this.proxyTransCommandRequest = proxyTransCommandRequest;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        throw new RuntimeException("Not implemented yet");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return this.dataCode.length;
        }
        throw new RuntimeException("Not implemented yet");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            this.encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding Proxy_Request", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public ProxyChoice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = ProxyChoice._ERR_NONE_SELECTED;
        this.proxyGetRequestList = null;
        this.proxyGetRequestRecord = null;
        this.proxySetRequestList = null;
        this.proxySetThenGetRequestList = null;
        this.proxyActionRequestList = null;
        this.proxyActionThenGetRequestList = null;
        this.proxyTransCommandRequest = null;
    }

    @Override
    public String toString() {
        return "Proxy_Request not fully implemented";
    }
}